﻿Public Class frmHangHoa

    Private Function LoadNSX() As DataTable
        Dim dt As New DataTable
        dt = NhaSanXuatDAO.Select_All()
        Return dt
    End Function

    Private Function LoadXX() As DataTable
        Dim dt As New DataTable
        dt = XuatXuDAO.Select_All()
        Return dt
    End Function

    'Load danh sach hang hoa
    Private Function LoadDSHH() As DataTable
        Dim dt As New DataTable
        dt = LoaiHangDAO.Select_All()
        Return dt
    End Function

    Private Sub Clear()
        txtMaLH.Text = ""
        txtTenLH.Text = ""
        txtMoTa.Text = ""
        txtBH.Text = ""
        'cmbNhaSX.SelectedText = ""
        'cmbXX.SelectedText = ""
    End Sub

    Private Sub frmHangHoa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cmbNhaSX.DataSource = LoadNSX()
        cmbNhaSX.DisplayMember = "TenNhaSX"
        cmbNhaSX.ValueMember = "MaNhaSX"
        cmbXX.DataSource = LoadXX()
        cmbXX.DisplayMember = "TenXX"
        cmbXX.ValueMember = "MaXX"
        dgvLH.DataSource = LoadDSHH()
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim _obj As New LoaiHangDTO
        _obj.MaLoai = txtMaLH.Text
        _obj.TenLoai = txtTenLH.Text
        _obj.NhaSanXuat = cmbNhaSX.SelectedValue
        _obj.XuatXu = cmbXX.SelectedValue
        _obj.BaoHanh = txtBH.Text
        _obj.MoTa = txtMoTa.Text
        If (LoaiHangDAO.Insert(_obj) = 1) Then
            MsgBox("Thêm thành công", MsgBoxStyle.OkOnly, "")
            dgvLH.DataSource = LoadDSHH()
            Clear()
        Else
            MsgBox("Không thể thêm loai hang nay", MsgBoxStyle.OkOnly, "")
        End If

    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim _maLoai As String
        _maLoai = txtMaLH.Text
        If (LoaiHangDAO.Delete(_maLoai) = 1) Then
            MsgBox("Xóa thành công", MsgBoxStyle.OkOnly, "")
            dgvLH.DataSource = LoadDSHH()
            Clear()
        Else
            MsgBox("Xóa thất bại", MsgBoxStyle.OkOnly, "")
        End If
    End Sub

    Private Sub dgvLH_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvLH.Click
        If (dgvLH.SelectedRows.Count > 0) Then
            txtMaLH.Text = dgvLH.SelectedRows(0).Cells(0).Value.ToString()
            txtTenLH.Text = dgvLH.SelectedRows(0).Cells(1).Value.ToString()
            txtMoTa.Text = dgvLH.SelectedRows(0).Cells(2).Value.ToString()
            cmbNhaSX.Text = dgvLH.SelectedRows(0).Cells(3).Value.ToString()
            cmbXX.Text = dgvLH.SelectedRows(0).Cells(4).Value.ToString()
            txtBH.Text = dgvLH.SelectedRows(0).Cells(5).Value.ToString()
        End If
    End Sub

    Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
        If dgvLH.SelectedRows.Count > 0 Then
            Dim _obj As New LoaiHangDTO
            _obj.MaLoai = txtMaLH.Text
            _obj.TenLoai = txtTenLH.Text
            _obj.NhaSanXuat = cmbNhaSX.SelectedValue
            _obj.XuatXu = cmbXX.SelectedValue
            _obj.BaoHanh = txtBH.Text
            _obj.MoTa = txtMoTa.Text
            If (LoaiHangDAO.Update(_obj) = 1) Then
                MsgBox("Cập nhật thành công", MsgBoxStyle.OkOnly, "")
                dgvLH.DataSource = LoadDSHH()
                Clear()
            Else
                MsgBox("Không thể cập nhật loai hang nay", MsgBoxStyle.OkOnly, "")
            End If
        Else
            MsgBox("Vui long chọn loai hang can cap nhật", MsgBoxStyle.OkOnly, "")
        End If
    End Sub
End Class